route.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET, dynamic } from "./route.js";
  11. describe("GET /api/branches/[branch]/[year]/months", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-months-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10"), {
  19. recursive: true,
  20. });
  21. });
  22. afterEach(async () => {
  23. process.env.NAS_ROOT_PATH = originalNasRoot;
  24. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  25. });
  26. it('exports dynamic="force-dynamic" (RHL-006)', () => {
  27. expect(dynamic).toBe("force-dynamic");
  28. });
  29. it("returns 401 when unauthenticated", async () => {
  30. getSession.mockResolvedValue(null);
  31. const res = await GET(
  32. new Request("http://localhost/api/branches/NL01/2024/months"),
  33. { params: Promise.resolve({ branch: "NL01", year: "2024" }) }
  34. );
  35. expect(res.status).toBe(401);
  36. expect(await res.json()).toEqual({
  37. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  38. });
  39. });
  40. it("returns 403 when branch user accesses a different branch", async () => {
  41. getSession.mockResolvedValue({
  42. role: "branch",
  43. branchId: "NL01",
  44. userId: "u1",
  45. });
  46. const res = await GET(
  47. new Request("http://localhost/api/branches/NL02/2024/months"),
  48. { params: Promise.resolve({ branch: "NL02", year: "2024" }) }
  49. );
  50. expect(res.status).toBe(403);
  51. expect(await res.json()).toEqual({
  52. error: { message: "Forbidden", code: "AUTH_FORBIDDEN_BRANCH" },
  53. });
  54. });
  55. it("returns months for a valid branch/year when allowed", async () => {
  56. getSession.mockResolvedValue({
  57. role: "admin",
  58. branchId: null,
  59. userId: "u2",
  60. });
  61. const res = await GET(
  62. new Request("http://localhost/api/branches/NL01/2024/months"),
  63. { params: Promise.resolve({ branch: "NL01", year: "2024" }) }
  64. );
  65. expect(res.status).toBe(200);
  66. expect(await res.json()).toEqual({
  67. branch: "NL01",
  68. year: "2024",
  69. months: ["10"],
  70. });
  71. });
  72. it("returns 400 when branch or year is missing (authenticated)", async () => {
  73. getSession.mockResolvedValue({
  74. role: "admin",
  75. branchId: null,
  76. userId: "u2",
  77. });
  78. const res = await GET(
  79. new Request("http://localhost/api/branches/NL01//months"),
  80. { params: Promise.resolve({ branch: "NL01", year: undefined }) }
  81. );
  82. expect(res.status).toBe(400);
  83. expect(await res.json()).toEqual({
  84. error: {
  85. message: "Missing required route parameter(s)",
  86. code: "VALIDATION_MISSING_PARAM",
  87. details: { params: ["year"] },
  88. },
  89. });
  90. });
  91. it("returns 404 when the year folder does not exist (authorized)", async () => {
  92. getSession.mockResolvedValue({
  93. role: "admin",
  94. branchId: null,
  95. userId: "u2",
  96. });
  97. const res = await GET(
  98. new Request("http://localhost/api/branches/NL01/2099/months"),
  99. { params: Promise.resolve({ branch: "NL01", year: "2099" }) }
  100. );
  101. expect(res.status).toBe(404);
  102. expect(await res.json()).toEqual({
  103. error: {
  104. message: "Not found",
  105. code: "FS_NOT_FOUND",
  106. details: { branch: "NL01", year: "2099" },
  107. },
  108. });
  109. });
  110. });